﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace DashFire
{
  public class AssetBundleAssetInfo
  {
    public void LoadXml(XmlDocument xml)
    {
      XmlNode xmlNode = xml.SelectSingleNode("AssetBundlesMD5Info");
      var IncrementAttributes = xmlNode.Attributes["Increment"];
      if (IncrementAttributes != null)
        IncrementIndex = int.Parse(IncrementAttributes.Value);
      foreach (XmlElement abElement in xmlNode.ChildNodes)
      {
        string name = abElement.GetAttribute("Name");
        XmlNodeList list = abElement.GetElementsByTagName("FileInfo");
        foreach (XmlElement fi in list)
        {
          FileInfo info = new FileInfo();
          info.Hash = fi.GetAttribute("Md5");
          info.AssetBundleName = name;
          string file_name = fi.GetAttribute("Name");
          if (mInfo.ContainsKey(file_name) == false)
          {
            mInfo.Add(file_name, info);
          }
        }
      }
    }

    public string GetAssetBundleName(string url)
    {
      FileInfo info;
      if (mInfo.TryGetValue(url, out info))
      {
        return info.AssetBundleName;
      }

      return string.Empty;
    }

    public bool IsIncrement(string path)
    {
      FileInfo info;
      if (mInfo.TryGetValue(path, out info) == false)
      {
        return true;
      }

      return false;
    }

    public int IncrementIndex
    {
      get; private set;
    }
    private class FileInfo
    {
      public string Hash;
      public string AssetBundleName;
    }
    Dictionary<string, FileInfo> mInfo = new Dictionary<string, FileInfo>();
  }
}
